using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;

namespace GameFrameworkLibraly.Helpers
{

    public static class IsolatedStorageSettingsExtensions
    {
        public static T GetSetting<T>(this IsolatedStorageSettings settings, string keyName)
        {
            return GetSetting(settings, keyName, default(T));
        }

        public static T GetSetting<T>(this System.IO.IsolatedStorage.IsolatedStorageSettings settings, string keyName, T defaultValue)
        {
            if (settings != null)
            {
                if (settings.Contains(keyName) == true)
                {
                    try
                    {
                        return (T)settings[keyName];
                    }
                    catch (InvalidCastException)
                    {
                    }
                }
            }

            return defaultValue;
        }

        public static void SaveSetting<T>(this IsolatedStorageSettings settings, string keyName, T value)
        {
            if (settings != null)
            {
                settings[keyName] = value;
                settings.Save();
            }
        }
    }
    
}
